﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace Algobox.JuicyLib.Apps.Stoxx
{
    public class StoxxClient
    {
        public StoxxClient(string username, string password)
        {
            Username = username;
            Password = password;
        }

        public string Username { get; private set; }
        public string Password { get; private set; }

        public bool TryGetEuropeanTMIClose(Dictionary<string, Structure.StoxxListing> data, out DateTime date)
        {
            date = DateTime.MinValue;

            string file;
            if (!TryGetStoxxFile(Files.OpeningTMIComponents.URL, out file))
                return false;

            if (Files.OpeningTMIComponents.TryParseFile(ref file, data, out date))
                return true;

            return false;
        }


        /// <summary>
        /// Fetches the dividends file from the Stoxx website then parses it to list
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool TryGetDividends(out List<Structure.StoxxDividend> data)
        {
            string file;
            if (!TryGetStoxxFile(Files.DividendForecast.URL, out file))
            {
                data = null;
                return false;
            }

            try
            {
                data = Files.DividendForecast.ParseFile(ref file);
                return true;
            }
            catch
            {
                data = null;
            }                

            return false;
        }

        /// <summary>
        /// Parses a Stoxx dividends file to a list of Structure.StoxxDividend
        /// </summary>
        /// <param name="file"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool TryGetDividends(string file, out List<Structure.StoxxDividend> data)
        {
            try
            {
                data = Files.DividendForecast.ParseFile(ref file);
                return true;
            }
            catch
            {
                data = null;
            }

            return false;
        }



        private bool TryGetStoxxFile(Uri url, out string file)
        {
            try
            {
                Algobox.JuicyLib.Net.WebPostRequest request = new Algobox.JuicyLib.Net.WebPostRequest("http://www.stoxx.com/download/data/change_files/login-user");

                //request.Referer = url.ToString();
                request.Cookie = "language=en; BALANCERID=b.cocoon0a1848; cug.test=Wiedersehen macht Freude";

                request.Add("viewFrom", "viewLOGIN");
                request.Add("cugname", "mystoxx");
                request.Add("forward", "/stoxxcom" + url.AbsolutePath);
                request.Add("referer", "/stoxxcom/mystoxx/login.html");
                request.Add("username", Username);
                request.Add("password", Password);
                request.Add("actionSUBMIT", "Login to MySTOXX");

                file = request.GetResponse();
                return true;
            }
            catch
            {
                file = String.Empty;
            }

            return false;
        }


    }
}
